﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Bson.IO;
using System.Globalization;

namespace MongoDb
{
    public class MongoPlay
    {
        static void Main(string[] args)
        {
            MongoPlay mongoPlay = new MongoPlay();
            mongoPlay.Start();
        }



        private void Start()
        {
            string connectionString = "mongodb://localhost";
            MongoClient client = new MongoClient(connectionString);
            MongoServer server = client.GetServer();

            MongoDatabase tradesDb = server.GetDatabase("trades");
            MongoCollection<LegalEntityAttribute> entAttrColl = tradesDb.GetCollection<LegalEntityAttribute>("legal_entity_attribute");

            //Creat a query
            IMongoQuery query = Query<LegalEntityAttribute>.EQ(e => e.Ident, 1);
            
            //Run Query FindOne
            LegalEntityAttribute aAttrCollItem = entAttrColl.FindOne(query);


            //Use cursor
            foreach (var item in entAttrColl.AsQueryable().Where(p => p.Ident < 1300))
            {
                Console.WriteLine("Id: " + item.Ident + ", Name:" + item.Name + ", Value:" + item.Value);
            }

        }


    }

    public class LegalEntityAttribute
    {
        public ObjectId Id    { get; set; }
        public int      Ident { get; set; }
        public string   Name  { get; set; }

        [BsonSerializer(typeof(StringOrInt32Serializer))]
        public string   Value { get; set; }
    }


    public sealed class StringOrInt32Serializer : BsonBaseSerializer
    {
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            var bsonType = bsonReader.CurrentBsonType;
            switch (bsonType)
            {
                case BsonType.Null:
                    bsonReader.ReadNull();
                    return null;
                case BsonType.String:
                    return bsonReader.ReadString();
                case BsonType.Int32:
                    return bsonReader.ReadInt32().ToString(CultureInfo.InvariantCulture);
                default:
                    var message = string.Format("Cannot deserialize BsonString or BsonInt32 from BsonType {0}.", bsonType);
                    throw new BsonSerializationException(message);
            }
        }

        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value != null)
            {
                bsonWriter.WriteString(value.ToString());
            }
            else
            {
                bsonWriter.WriteNull();
            }
        }
    }


}
